﻿
using System;
using System.Collections.Generic;
using System.Linq;
using RBAC.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using RBAC.DAL.Models;


namespace RBAC.DAL.Dal
{
    public class PermissionDAL
    {
        private readonly ISession _session;
        private readonly int _projectId;
        public PermissionDAL(ISession session, int projectId)
        {
            _session = session;
            _projectId = projectId;
        }

        public List<Permission> GetAll(PermissionSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(Permission));

            if (searchCriteria != null)
            {
                #region Search
                //Pk
                if (!string.IsNullOrEmpty(searchCriteria.PermissionName))
                {
                    if (searchCriteria.EqPk.GetValueOrDefault())
                    {
                        criteria.Add(Restrictions.Eq("PermissionName", searchCriteria.PermissionName));
                    }
                    else
                    {
                        criteria.Add(Restrictions.Like("PermissionName", searchCriteria.PermissionName, MatchMode.Anywhere));
                    }
                }
                //lookup to parent
                if (null != searchCriteria.Role)
                {
                    criteria.Add(Restrictions.Eq("Role", searchCriteria.Role));
                }
                if (searchCriteria.IsActive != null)
                {
                    criteria.Add(Restrictions.Eq("IsActive", searchCriteria.IsActive));
                }
                if (searchCriteria.ProjectId != null)
                {
                    criteria.Add(Restrictions.Eq("ProjectId", searchCriteria.ProjectId));
                }
                #endregion

                #region Sort
                if (searchCriteria.IsSort)
                    criteria.AddOrder(searchCriteria.Order);
                #endregion
            }
            return criteria.List<Permission>().ToList();
        }

        public void Add(Permission permission)
        {
            if (permission != null)
            {
                permission.CreateDate = DateTime.Now;
                permission.ProjectId = _projectId;
                //if (permission.CreateBy != null) permission.CreateBy = _session.Get<User>(permission.CreateBy.Id);
            }
            _session.Save(permission);
        }

        public void Update(Permission permission)
        {

            // chlidren

            var obj = _session.Get<Permission>(permission.Id);
            if (obj == null)
                throw new Exception("Update Not Success");

            permission.CreateDate = obj.CreateDate;
            permission.CreateById = obj.CreateById;
            permission.ProjectId = obj.ProjectId;
            _session.Merge(permission);

        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE RBAC.DAL.Entities.Permission where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");
        }
    }
}

